﻿#include "reportManager.h"


QString extractFileName(QString path){
    path.replace("\\", "/");
    return path.split("/").back();
}
QString extractPathName(QString path){
    QString filename = extractFileName(path);
    if(!filename.contains(".")){
        return path;
    }else{
        return path.mid(0, path.size() - filename.size());
    }
}




ReportManager::ReportManager()
{
    _exportType = WORD;
    _exportPath = "./datao";

    // 从数据库中获取默认导出路径
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = "Select * From ExportInfo";
    query.exec(sql);
    int i = 0;
    while(query.next()){
        i ++;
        _exportPath = query.value("Path").toString();
        _exportType = (ExportType)query.value("Type").toInt();
    }
    if(i == 0){
        sql = QString("Insert Into ExportInfo Values ('%1', '%2')").arg(_exportPath, QString::number(_exportType));
        query.exec(sql);
    }

}

ReportManager& ReportManager::I(){
    static ReportManager instance;
    return instance;
}

bool ReportManager::setExportPath(QString path){
    _exportPath = path;

    qDebug() << "setExportPath " + path;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Update ExportInfo Set Path='%1'").arg(path);
    query.exec(sql);


    return true;
}

bool ReportManager::setExportType(ExportType type){
    _exportType = type;

    qDebug() << QString("setExportType ") + type;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Update ExportInfo Set Type='%1'").arg(QString::number(type));
    query.exec(sql);

    return true;
}

QString ReportManager::getExportPath(){
    return _exportPath;
}
ReportManager::ExportType ReportManager::getExportType(){
    return _exportType;
}

int ReportManager::exportReport(){
    return exportReport(_exportPath + "/" + QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + _typeSuffix[_exportType]);
}

bool ReportManager::setAutoExport(bool state){
    _autoExport = state;
    return true;
}

bool ReportManager::isAutoExport(){
    return _autoExport;
}


int ReportManager::exportReport(QString path){

    int res;

    stReportInfo info = queryReportInfo();

    QString suffix = extractFileName(path).split(".").back();


    if(suffix == "docx"){
        WordExporter* we = new WordExporter;
        res = we->exportWord(extractPathName(path), extractFileName(path), info.expInfo, info.logInfo);
        delete we;
    }else{
        ExcelExporter* ee = new ExcelExporter;
        res = ee->exportExcel(extractPathName(path), extractFileName(path), info.expInfo, info.logInfo);
    }

    return res;
}

ReportManager::stReportInfo ReportManager::queryReportInfo(){
    stReportInfo res;
    if(_expInfo["Name"] == ""){
        _expInfo["Name"] = QDateTime::currentDateTime().toString("yyMMdd");
    }
    _expInfo["Date"] = QDateTime::currentDateTime().toString("yyMMdd");
    _expInfo["EndTime"] = QDateTime::currentDateTime().toString("yyMMdd_hhmmss");
    res.expInfo = _expInfo;


    // extern QMap<int, QVariantMap> expLogInfo;
    for(const auto &key : expLogInfo.keys()){
        QStringList line;
        QVariantMap map = expLogInfo[key];
        if(!map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime") || !map.contains("FinishTime")){
            qDebug() << "ERROR: [Class]ReportManager [Function]QueryReportInfo - expLogInfo loss key";
            continue;
        }
        line.push_back(map["FinishTime"].value<QString>());
        line.push_back(map["Step"].value<QString>());
        line.push_back(map["ActionInfo"].value<QString>());
        line.push_back(map["Status"].value<QString>());
        res.logInfo.push_back(line);
    }


    // 临时


    return res;
}

void ReportManager::updateExperimentInfo(QMap<QString, QString> info){
    _expInfo = info;
}
QMap<QString, QString> ReportManager::getExperimetnInfo(){
    return _expInfo;
}


